package com.qf.direct;

import com.qf.util.RabbitMQUtil;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

/**
 * @author zjw
 * @description
 * @date 2021/11/8 16:29
 */
public class Consumer {

    @Test
    public void consume1() throws Exception {
        //1. conn
        Connection connection = RabbitMQUtil.getConnection();
        //2. channel
        Channel channel = connection.createChannel();
        //3. queueDeclare
        channel.queueDeclare("routing_queue1",true,false,false,null);

        //4. consume
        DefaultConsumer callback = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1号获取消息：" + new String(body));
                // 手动ack
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume("routing_queue1",false,callback);
        System.out.println("消费者1号开始监听");
        //5. System.in
        System.in.read();
    }



    @Test
    public void consume2() throws Exception {
        //1. conn
        Connection connection = RabbitMQUtil.getConnection();
        //2. channel
        Channel channel = connection.createChannel();
        //3. queueDeclare
        channel.queueDeclare("routing_queue2",true,false,false,null);

        //4. consume
        DefaultConsumer callback = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2号获取消息：" + new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume("routing_queue2",false,callback);
        System.out.println("消费者2号开始监听");
        //5. System.in
        System.in.read();
    }
}
